package com.supms.course.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.supms.course.domain.TestReviewEval;
import com.supms.course.mapper.TestReviewMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.supms.course.mapper.TestReviewItemsMapper;
import com.supms.course.domain.TestReviewItems;
import com.supms.course.service.ITestReviewItemsService;

/**
 * 试卷复查评价Service业务层处理
 * 
 * @author LiangHao
 * @date 2022-03-08
 */
@Service
public class TestReviewItemsServiceImpl implements ITestReviewItemsService 
{
    @Autowired
    private TestReviewItemsMapper testReviewItemsMapper;

    @Autowired
    private TestReviewMapper testReviewMapper;

    /**
     * 查询试卷复查评价
     * 
     * @param id 试卷复查评价主键
     * @return 试卷复查评价
     */
    @Override
    public TestReviewItems selectTestReviewItemsById(Long id)
    {
        return testReviewItemsMapper.selectTestReviewItemsById(id);
    }

    /**
     * 查询试卷复查评价列表
     * 
     * @param testReviewItems 试卷复查评价
     * @return 试卷复查评价
     */
    @Override
    public List<TestReviewItems> selectTestReviewItemsList(TestReviewItems testReviewItems)
    {
        return testReviewItemsMapper.selectTestReviewItemsList(testReviewItems);
    }

    /**
     * 新增试卷复查评价
     * 
     * @param testReviewItems 试卷复查评价
     * @return 结果
     */
    @Override
    public int insertTestReviewItems(TestReviewItems testReviewItems)
    {
        testReviewItems.setCreateTime(DateUtils.getNowDate());
        return testReviewItemsMapper.insertTestReviewItems(testReviewItems);
    }

    /**
     * 修改试卷复查评价
     * 
     * @param testReviewItems 试卷复查评价
     * @return 结果
     */
    @Override
    public int updateTestReviewItems(TestReviewItems testReviewItems)
    {
        testReviewItems.setUpdateTime(DateUtils.getNowDate());
        return testReviewItemsMapper.updateTestReviewItems(testReviewItems);
    }

    /**
     * 批量删除试卷复查评价
     * 
     * @param ids 需要删除的试卷复查评价主键
     * @return 结果
     */
    @Override
    public int deleteTestReviewItemsByIds(Long[] ids)
    {
        return testReviewItemsMapper.deleteTestReviewItemsByIds(ids);
    }

    /**
     * 删除试卷复查评价信息
     * 
     * @param id 试卷复查评价主键
     * @return 结果
     */
    @Override
    public int deleteTestReviewItemsById(Long id)
    {
        return testReviewItemsMapper.deleteTestReviewItemsById(id);
    }

    /**
     * 添加本科试卷复查评价（添加中间表）
     * @param testReviewItemsList 接收到已评价的评价表
     * @return 结果
     */
    @Override
    public int addTestReviewEval(List<TestReviewItems> testReviewItemsList) {

        List<TestReviewEval> testReviewEvalList = new ArrayList<>();
        Long testReviewId = null;

        for (TestReviewItems testReviewItems : testReviewItemsList) {
            if (testReviewItems.getMySituation() != null) {
                TestReviewEval testReviewEval = new TestReviewEval();
                testReviewId = testReviewItems.getTestReviewId();
                testReviewEval.setTestReviewId(testReviewId);
                testReviewEval.setTestReviewItemsId(testReviewItems.getId());
                testReviewEval.setSituation(testReviewItems.getMySituation());
                testReviewEval.setCreateTime(DateUtils.getNowDate());
                // 将收集到的评价数据收集到评价表对象，并将对象一条一条放到列表中
                testReviewEvalList.add(testReviewEval);
            } else return 0;
        }

        // 将试卷复查表中的评价标记改为已评价（"1"）
        testReviewMapper.updateReviewEvalFlagById(testReviewId);
        return testReviewItemsMapper.addTestReviewEval(testReviewEvalList);
    }
}
